package edu.cicese.alz;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 13/01/12
 * Time: 11:22 AM
 */
public class GPSUtilities {
	private static final double R = 6371000.0;  // earths mean radius in m

	// Spherical Law of Cosines
	public static double getSLCDistFrom(double lat1, double lon1, double lat2, double lon2) {
//		double dLat = Math.toRadians(lat2 - lat1);
		double dLon = Math.toRadians(lon2 - lon1);
		lat1 = Math.toRadians(lat1);
		lat2 = Math.toRadians(lat2);

		return R * Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(dLon));

//		var d = Math.acos(Math.sin(lat1)*Math.sin(lat2) +
//                  Math.cos(lat1)*Math.cos(lat2) *
//                  Math.cos(lon2-lon1)) * R;
	}

	// Haversine
	public static double getHaversineDistFrom(double lat1, double lon1, double lat2, double lon2) {
		double dLat = Math.toRadians(lat2 - lat1);
		double dLon = Math.toRadians(lon2 - lon1);
		double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
				   Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
				   Math.sin(dLon / 2) * Math.sin(dLon / 2);
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
		return R * c;

//		var R = 6371; // km
//		var dLat = (lat2-lat1).toRad();
//		var dLon = (lon2-lon1).toRad();
//		var lat1 = lat1.toRad();
//		var lat2 = lat2.toRad();
//
//		var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
//				Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
//		var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
//		var d = R * c;
	}
}
